package cn.wowjoy.pda.android_pharmacy.mvvm.presenter;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.doorPatientInfo.DoorPatientInfoBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.updateBoxInfo.UpdateBoxNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.updatePatientInfo.UpdateDoorPatientNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.updatePatientInfo.UpdateHospitalStaticNode;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.DoorTakeProgressViewModel;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by sinyo on 2017/7/24.
 */

public class DoorTakeProgressPresenter extends BasePresenter<DoorTakeProgressViewModel> {

    ArrayList<DoorPatientInfoBeen> FitstTotal;
    //    private ArrayList<DoorPatientInfoBeen> mTestBeen=new ArrayList<>();
    private ApiService mApiService;
    private MDialog waitDialog;
    public String DJJLID;
    public String BRDAID;
    public String BRZYID;
    public  int type;
    public List<String> DJJLIDs;
    @Override
    public void onCreatePresenter() {
        loadData(false);
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
    }

    @Override
    public void loadData(boolean ref) {
        type = activity.getIntent().getExtras().getInt("type",0);
        if(type == 1){
            DJJLIDs= (List<String>) activity.getIntent().getExtras().getSerializable("DJJLIDs");
        }else{
            DJJLID = activity.getIntent().getExtras().getString("DJJLID");
        }
        FitstTotal = (ArrayList<DoorPatientInfoBeen>) activity.getIntent().getExtras().getSerializable("total");

        BRDAID = activity.getIntent().getExtras().getString("BRDAID");
        BRZYID = activity.getIntent().getExtras().getString("BRZYID");
//        Log.e(TAG, "BRZYID: "+BRZYID );
//        Log.e("PXY", "DoorTakeProgressPresenter: " + FitstTotal.toString() +"    FitstTotal."+FitstTotal.size());
        if (FitstTotal != null) {
            viewModel.show(FitstTotal);
        }
    }
    //TODO:扫码获取的属性与当前集合中的要被移除的Bean进行对比
    //1是没有设置药品码或者药监码
    //2是设置了码 但是没有匹配
    //0是正确的
    public int confimYPSPBM2(String YPSPBM){
//        Log.e(TAG,"viewModel.remove:"+viewModel.remove.toString());
        if(null ==viewModel.remove && null == viewModel.remove.getYPSPBM()){
            return 1;
        }
        if(viewModel.remove.getYPSPBM().equals(YPSPBM)){
           return 0;
        }
        return 2;
    }
    //TODO:扫码获取的属性与当前集合中的要被移除的Bean进行对比
    public boolean confimYPSPBM(String YPSPBM){
//        Log.e(TAG,"viewModel.remove:"+viewModel.remove.toString());
        if(null !=viewModel.remove && null != viewModel.remove.getYPSPBM()){
            if(viewModel.remove.getYPSPBM().equals(YPSPBM)){
                return true;
            }
        }
        return false;
    }
    //TODO:扫码获取的属性与当前集合中的要被移除的Bean进行对比
    public boolean confimYPJGBM(String YPJGBM){
//        Log.e(TAG,"viewModel.remove:"+viewModel.remove.toString());
        if(null !=viewModel.remove && null != viewModel.remove.getYPJGBM()){
            if(viewModel.remove.getYPJGBM().equals(YPJGBM)){
                return true;
            }
        }
        return false;
    }
    //TODO:扫码获取的属性与当前集合中的要被移除的Bean进行对比
    public boolean confimKFWZMC(String KFWZMC){
//        Log.e(TAG,"viewModel.remove:"+viewModel.remove.toString());
        if(null !=viewModel.remove && null != viewModel.remove.getKFWZMC() && viewModel.remove.getKFWZMC().length() >=7){
            if(viewModel.remove.getKFWZMC().substring(0,7).equals(KFWZMC)){
                return true;
            }
        }
        return false;
    }

    //TODO:更新药单  药单已完成  PDAU01
//   DJJLID	处方号	Varchar2(20)
//    DJDQZT	单据当前状态 	Number(1)
//    CZYHID	操作用户ID	Varchar2(20)
    public void update(String DJJLID,String DJDQZT, String CZYHID) {
        Disposable disposable = null;
        boolean result;
        if(null != BRDAID && "null".equals(BRDAID)){
            disposable = mApiService.doXml(XmlUtils.prepare(UpdateDoorPatientNode.getContent("PDAU01", DJJLID, DJDQZT, CZYHID)))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {

                        @Override
                        protected void onStart() {
                            if(null == waitDialog){
                                waitDialog = CreateDialog.waitingDialog(activity);
                            }
                        }

                        @Override
                        public void onNext(RoleInfoResponseEnvelope xmlData) {
                            CreateDialog.dismiss(activity);
                            if ("-1".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                                mOnSubmitResult.submitFail("该单据已抓完药更新过状态, 请核对！");
                            } else if ("0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                                mOnSubmitResult.submitSuccess();
                            }else {
                                mOnSubmitResult.submitFail("服务器异常");
                            }
                        }

                        @Override
                        public void onComplete() {
                        }

                        @Override
                        public void onError(Throwable e) {
//                       waitDialog.dismiss();
                            CreateDialog.dismiss(activity);
                            if(null != e && null != e.getMessage()){
                                Log.e("Throwable", e.getMessage());
                                new MyToast(activity).showinfo(e.getMessage());
                            }
                        }
                    });
        }else{

            disposable = mApiService.doXml(XmlUtils.prepare(UpdateHospitalStaticNode.getCYTakeContent("PDAU03",  Long.parseLong((DJJLID+BRDAID).trim()), DJDQZT, CZYHID,"5")))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {

                        @Override
                        protected void onStart() {
                            if(null == waitDialog){
                                waitDialog = CreateDialog.waitingDialog(activity);
                            }
                        }

                        @Override
                        public void onNext(RoleInfoResponseEnvelope xmlData) {
                            CreateDialog.dismiss(activity);
                            if ("-1".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                                mOnSubmitResult.submitFail("该单据已抓完药更新过状态, 请核对！");
                            } else if ("0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                                mOnSubmitResult.submitSuccess();
                            }else {
                                mOnSubmitResult.submitFail("服务器异常");
                            }
                        }

                        @Override
                        public void onComplete() {
                        }

                        @Override
                        public void onError(Throwable e) {
//                       waitDialog.dismiss();
                            CreateDialog.dismiss(activity);
                            if(null != e && null != e.getMessage()){
                                Log.e("Throwable", e.getMessage());
                                new MyToast(activity).showinfo(e.getMessage());
                            }
                        }
                    });
        }

        addDisposable(disposable);
    }

    //    KFWZMC	药品摆放位置
    public void setPlace(final String KFWZMC) {
        Disposable disposable = null;
        boolean result;
        if(null != BRDAID && "null".equals(BRDAID)){
            disposable = mApiService.doXml(XmlUtils.prepare(UpdateBoxNode.getCYContent("PDAU02", DJJLID, KFWZMC,"7", PreferenceManager.getInstance().getLUserId(),"0")))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {

                        @Override
                        protected void onStart() {
                            if(null == waitDialog){
                                waitDialog = CreateDialog.waitingDialog(activity);
                            }
                        }
                        @Override
                        public void onNext(RoleInfoResponseEnvelope xmlData) {
                            CreateDialog.dismiss(activity);
//                               waitDialog.dismiss();
                    //        Log.e("PPP", "onNext: "+ xmlData.body.roleInfoResponse.toString());
                            if (null != mOnScanResult) {
                                if (!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                                    mOnScanResult.scanFail("设置缓存柜失败");
                                    return;
                                }
                                mOnScanResult.scanSuccess(KFWZMC);
                            }
                        }
                        @Override
                        public void onComplete() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            CreateDialog.dismiss(activity);
                            if(null != e && null != e.getMessage()){
                                Log.e("Throwable", e.getMessage());
                                new MyToast(activity).showinfo(e.getMessage());
                            }
                        }
                    });
        }else{
            disposable = mApiService.doXml(XmlUtils.prepare(UpdateBoxNode.getCYContent("PDAU02", DJJLID, KFWZMC,"8", PreferenceManager.getInstance().getLUserId(),BRZYID)))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {

                        @Override
                        protected void onStart() {
                            if(null == waitDialog){
                                waitDialog = CreateDialog.waitingDialog(activity);
                            }
                        }
                        @Override
                        public void onNext(RoleInfoResponseEnvelope xmlData) {
                            CreateDialog.dismiss(activity);
//                               waitDialog.dismiss();
                            if (null != mOnScanResult) {
                                if (!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                                    mOnScanResult.scanFail("设置缓存柜失败");
                                    return;
                                }
                                mOnScanResult.scanSuccess(KFWZMC);
                            }
                        }
                        @Override
                        public void onComplete() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            CreateDialog.dismiss(activity);
                            if(null != e && null != e.getMessage()){
                                Log.e("Throwable", e.getMessage());
                                new MyToast(activity).showinfo(e.getMessage());
                            }
                        }
                    });
        }

        addDisposable(disposable);
    }

    private OnScanResult mOnScanResult;

    public void registeOnScanResultListener(OnScanResult mOnScanResult) {
        this.mOnScanResult = mOnScanResult;
    }

    public void unRegisteOnScanResultListener() {
        this.mOnScanResult = null;
    }
    //扫码监听
    public interface OnScanResult {
        void scanSuccess(String KFWZMC);
        void scanFail(String errMsg);
    }

    //药单更新监听
    private OnSubmitResult mOnSubmitResult;

    public void registeOnSubmitResultListener(OnSubmitResult mOnScanResult) {
        this.mOnSubmitResult = mOnScanResult;
    }

    public void unRegisteOnSubmitResultListener() {
        this.mOnSubmitResult = null;
    }
    //药单更新监听
    public interface OnSubmitResult {
        void submitSuccess();
        void submitFail(String errMsg);
    }
}
